﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HaiFengShop.Tools.WeChat
{
    /// <summary>
    /// 微信OpenId信息
    /// </summary>
    public class WeChatOpenIdInfo
    {
        /// <summary>
        /// 获取小程序OpenId信息
        /// </summary>
        /// <param name="code">code兑换码</param>
        /// <param name="appId">小程序APPID</param>
        /// <param name="secret">小程序秘钥</param>
        /// <returns></returns>
        public async static Task<OpenIdModel> GetXCXOpenIdInfo(string code, string appId, string secret)
        {
            string requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
            try
            {
                using (HttpClient http = new HttpClient())
                {
                    var res = await http.GetAsync(requestUrl);
                    var result = await res.Content.ReadAsStringAsync();
                    var openIdModel = JsonConvert.DeserializeObject<OpenIdModel>(result);
                    return openIdModel;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取开放平台-用户OpenId信息（公众号，PC/H5）
        /// </summary>
        /// <param name="code">code兑换码</param>
        /// <param name="appId">公众号APPID</param>
        /// <param name="secret">公众号秘钥</param>
        /// <returns></returns>
        public async static Task<OpenIdModel> GetKFPTOpenIdInfo(string code, string appId, string secret)
        {
            string requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
            try
            {
                using (HttpClient http = new HttpClient())
                {
                    var res = await http.GetAsync(requestUrl);
                    var result = await res.Content.ReadAsStringAsync();
                    var openIdModel = JsonConvert.DeserializeObject<OpenIdModel>(result);
                    return openIdModel;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

    }

    /// <summary>
    /// OpenId对象模型
    /// </summary>
    public class OpenIdModel
    {
        /// <summary>
        /// 用户唯一标识
        /// </summary>
        public string openid { get; set; }

        /// <summary>
        /// 用户在开放平台的唯一标识符，在满足 UnionID 下发条件的情况下会返回
        /// </summary>
        public string unionid { get; set; }

        /// <summary>
        /// 会话密钥
        /// </summary>
        public string session_key { get; set; }

        /// <summary>
        /// access_token接口调用凭证超时时间，单位（秒）
        /// </summary>
        public long expires_in { get; set; }

        /// <summary>
        /// 接口调用凭证
        /// </summary>
        public string access_token { get; set; }



        /// <summary>
        /// 用户刷新access_token
        /// </summary>
        public string refresh_token { get; set; }

        /// <summary>
        /// 用户授权的作用域，使用逗号（,）分隔
        /// </summary>
        public string scope { get; set; }





        /// <summary>
        /// 错误返回代码
        /// </summary>
        public string errcode { get; set; }

        /// <summary>
        /// 错误返回文本
        /// </summary>
        public string errmsg { get; set; }
    }


}
